package com.tiancheng.trade.settlement.controller.admin;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.exception.error.ArgumentErrorInfoEnum;
import com.tiancheng.trade.settlement.model.settlement.SettlementPayAcctConfig;
import com.tiancheng.trade.settlement.service.settlement.ISettlementPayAcctConfigService;
import com.tiancheng.trade.settlement.util.Res;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 清分对帐
 *
 * @author chris.shao
 * @since 2024-4-21
 */
@RestController
@RequestMapping("/admin/settlement/config")
public class SettlementConfigController {

    @Autowired
    private ISettlementPayAcctConfigService settlementPayAcctConfigService;


    @GetMapping("/list")
    public Result queryList(SettlementPayAcctConfig settlementPayAcctConfig) throws BusinessException {

        if (StringUtils.isEmpty(settlementPayAcctConfig.getPayChannel())) {
            return Result.fail("请选择支付渠道");
        }
        return Result.success(settlementPayAcctConfigService.queryList(settlementPayAcctConfig));
    }

    @GetMapping("/{id}")
    public Result querySingle(@PathVariable("id") Long id) throws BusinessException {
        if (id == null) {
            return Result.fail("参数错误");
        }
        return Result.success(settlementPayAcctConfigService.query(id));
    }

    @PostMapping("/")
    public Result create(@RequestBody SettlementPayAcctConfig settlementPayAcctConfig) throws BusinessException {
        return Res.judge(settlementPayAcctConfigService.create(settlementPayAcctConfig), "新增");
    }

    @PutMapping("/")
    public Result update(@RequestBody SettlementPayAcctConfig settlementPayAcctConfig) throws BusinessException {
        Assert.isTrue(settlementPayAcctConfig.getId() != null, ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        return Res.judge(settlementPayAcctConfigService.update(settlementPayAcctConfig), "修改");

    }

    @GetMapping("/queryConfig")
    public Result queryConfigByCostType(SettlementPayAcctConfig param) throws BusinessException {
        List<SettlementPayAcctConfig> list = settlementPayAcctConfigService.queryConfigByCostType(param);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.success("1");
        }
        return Result.success("0");
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        return Res.judge(settlementPayAcctConfigService.remove(id), "删除");
    }
}
